<div class="row-fluid">
	<div class="span12">
		<ul class="breadcrumb">
			<li>
				<a href="index.php">
					<i class="radmin-icon radmin-home"></i>
					Dashboard
				</a>
				<span class="divider">/</span>
			</li>
			<li>
				<a href="whm-accounts.php">
					<i class="radmin-icon radmin-screen"></i>
					Web Hosts
				</a>
				<span class="divider">/</span>
			</li>
			<li class="active">
				<i class="radmin-icon radmin-enter"></i>
				cPanel Accounts
			</li>
		</ul>
	</div>
</div>

<div class="row-fluid">
	<div class="span12">
		<h4 class="title">Web Hosts | Host Login Setup</h4>
		<div class="squiggly-border"></div>
		<div class="row-fluid">
			<div class="span12 login_detail_form_info hide" id="login_detail_success_info">
				<div class="alert alter-block alert-success">
		<!--			<button type="button" class="close" data-dismiss="alert">&times;</button>-->
					<h4>Success</h4>
					<div class="content">Host Login Detail has been saved!</div>
				</div>
			</div>
		</div>
		<div class="row-fluid">
			<div class="span12 login_detail_form_info hide" id="login_detail_error_info">
				<div class="alert alter-block alert-error">
		<!--			<button type="button" class="close" data-dismiss="alert">&times;</button>-->
					<h4>Error</h4>
					<div class="content"></div>
				</div>
			</div>
		</div>
		
		<?php $form = $this->beginWidget('CActiveForm', array(
			'id' => 'host-login-add-form',
			'enableClientValidation' => true,
			'clientOptions' => array(
				'validateOnSubmit' => true,
			),
		)); ?>
			<fieldset>
<!--				<?php //echo $form->dropDownList($hostLogin, 'id_host', $hostLogin->listHostOptions()); ?><br />-->
				<?php echo CHtml::dropDownList('HostLogin[id_host]', $preSelectedHostId, $hostLogin->listHostOptions()) ?><br />
				
				<h5>Login Details</h5>
				<?php echo $form->dropDownList($hostLogin, 'protocol', $hostLogin->listProtocolOptions(), array('class' => 'fields')); ?>
				<?php echo $form->textField($hostLogin, 'request_uri', array('class' => 'fields', 'placeholder' => 'Request URI')); ?><br />
				
				<?php echo $form->dropDownList($hostLogin, 'method', $hostLogin->listMethodOptions(), array('class' => 'fields')); ?><br />
				
				<?php echo $form->textField($hostLogin, 'field_username', array('class' => 'fields', 'placeholder' => 'Username Field')); ?>
				<?php echo $form->textField($hostLogin, 'username', array('class' => 'fields', 'placeholder' => 'Username')); ?><br />
				<?php echo $form->textField($hostLogin, 'field_password', array('class' => 'fields', 'placeholder' => 'Password Field')); ?>
				<?php echo $form->textField($hostLogin, 'password', array('class' => 'fields', 'placeholder' => 'Password')); ?><br />

				<?php echo $form->textField($hostLogin, 'extra_variables', array('class' => 'fields', 'placeholder' => 'Extra Variables: i.e. &x=y&a=b')); ?><br />


				<?php echo CHtml::ajaxSubmitButton(
					'Save Details', 
					app()->createUrl('whm/host/logindetail'),
					array(
						'type' => 'post',
						'beforeSend' => 'function(){
							$("#host_login_detail_btn").button("loading");
						}',
						'success' => 'function(ret){
							$(".login_detail_form_info").hide();
							$("#host_login_detail_btn").button("reset");
							
							if(ret["result"]){
								$("#login_detail_success_info").blink();
							}else{
								$("#login_detail_error_info").blink({
									"beforeCall": function(){
										$("#login_detail_error_info .content").html("");
										$.each(ret["errors"], function(attrName, error){
											$(error).each(function(k, e){
												var p = $("<p />").addClass("text-error").append(attrName+": "+e);
												$("#login_detail_error_info .content").append(p);
											});
										});
									}
								});
							}
						}',
						'dataType' => 'json'
					),
					array(
						'id' => 'host_login_detail_btn',
						'type' => 'button', 
						'class' => 'btn btn-small btn-primary',
						'data-loading-text' => 'Saving...'
					)
				); ?>
			</fieldset>
		<?php $this->endWidget(); ?>
		
		<script>
			$('#HostLogin_id_host').change(function(){
				$('#host-login-add-form .fields').each(function(k, v){
					$(v).data('OLD_placeholder', $(v).attr('placeholder'));
					$(v).attr('placeholder', 'Loading...');
					$(v).attr('disabled', true);
					$(v).val('');
				});
				
				var obj = $(this);
				$.ajax({
					url: <?php echo json_encode(url('whm/host/getlogindetail')); ?>,
					type: 'get',
					data: {
						'id_host': obj.val()
					},
					success: function(ret){
						$('#host-login-add-form .fields').each(function(k, v){
							$(v).attr('placeholder', $(v).data('OLD_placeholder'));
							$(v).removeAttr('disabled');
						});
						
						if(ret){
							$.each(ret, function(k, v){
								$('#HostLogin_'+k).val(v);
							});
						}else{
							$('#host-login-add-form .fields').val('');
						}
					},
					dataType: 'json'
				});
			}).change();
		</script>
		
	</div>
</div>